package vmcs.customer.state;

import vmcs.common.VMCSException;
import vmcs.controller.TransactionController;

public class ChangeGivenState implements State{

	private TransactionController transactionController;
	private static ChangeGivenState instance;

	private ChangeGivenState(TransactionController transactionController) {
		this.transactionController = transactionController; 
	}

	public static ChangeGivenState getInstance(TransactionController transactionController) {
		if (instance == null) 
			instance = new ChangeGivenState(transactionController);
		return instance;
	}

	@Override
	public void selectDrink(int drinkSelected) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dispenseDrink(int drinkSelected) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void giveChange(int changeRequired) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void storeCash() {
		boolean cashStored =  false;
		try {
			cashStored = transactionController.getCoinReceiver().storeCash();
		} catch (VMCSException e) {
			if(!cashStored){
				transactionController.terminateMachine();
				return;
			}
		}
		
	
		
		transactionController.setState(transactionController.getIdleState());
		transactionController.initialize();
		transactionController.getDispenseController().allowSelection(true);
	}


	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insertCoin(int value) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void terminateMachine() {
	
			transactionController.getDispenseController().allowSelection(false);

			transactionController.getCoinReceiver().stopReceive();

			transactionController.getCoinReceiver().refundCash();  

			transactionController.setState(transactionController.getMachineTerminatedState());
		
	} 

	
	
}
